<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.student.dao.StudentDao" %>
<%@ page import="com.student.model.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>查询结果</title>
    <style>
        .container {
            width: 90%;
            margin: 20px auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .no-data {
            text-align: center;
            padding: 20px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>查询结果</h2>
        <%
        request.setCharacterEncoding("UTF-8");
        
        StringBuilder condition = new StringBuilder();
        ArrayList<String> conditions = new ArrayList<String>();
        
        String id = request.getParameter("id");
        if(id != null && !id.isEmpty()) {
            conditions.add("id = " + id);
        }
        
        String name = request.getParameter("name");
        if(name != null && !name.isEmpty()) {
            conditions.add("name = '" + name + "'");
        }
        
        String sex = request.getParameter("sex");
        if(sex != null && !sex.isEmpty()) {
            conditions.add("sex = '" + sex + "'");
        }
        
        String age = request.getParameter("age");
        if(age != null && !age.isEmpty()) {
            conditions.add("age = " + age);
        }
        
        String minWeight = request.getParameter("minWeight");
        String maxWeight = request.getParameter("maxWeight");
        if(minWeight != null && !minWeight.isEmpty() && maxWeight != null && !maxWeight.isEmpty()) {
            conditions.add("weight BETWEEN " + minWeight + " AND " + maxWeight);
        } else if(minWeight != null && !minWeight.isEmpty()) {
            conditions.add("weight >= " + minWeight);
        } else if(maxWeight != null && !maxWeight.isEmpty()) {
            conditions.add("weight <= " + maxWeight);
        }
        
        String minHeight = request.getParameter("minHeight");
        String maxHeight = request.getParameter("maxHeight");
        if(minHeight != null && !minHeight.isEmpty() && maxHeight != null && !maxHeight.isEmpty()) {
            conditions.add("height BETWEEN " + minHeight + " AND " + maxHeight);
        } else if(minHeight != null && !minHeight.isEmpty()) {
            conditions.add("height >= " + minHeight);
        } else if(maxHeight != null && !maxHeight.isEmpty()) {
            conditions.add("height <= " + maxHeight);
        }
        
        if(!conditions.isEmpty()) {
            condition.append(String.join(" AND ", conditions));
        }
        
        StudentDao dao = new StudentDao();
        List<Student> students = condition.length() > 0 ? 
                               dao.findStudentsByCondition(condition.toString()) : 
                               dao.findAllStudents();
        
        if(students != null && !students.isEmpty()) {
        %>
        <table>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>体重(kg)</th>
                <th>身高(cm)</th>
            </tr>
            <%
            for(Student student : students) {
            %>
            <tr>
                <td><%=student.getId()%></td>
                <td><%=student.getName()%></td>
                <td><%=student.getSex()%></td>
                <td><%=student.getAge()%></td>
                <td><%=student.getWeight()%></td>
                <td><%=student.getHeight()%></td>
            </tr>
            <%
            }
            %>
        </table>
        <%
        } else {
        %>
        <div class="no-data">
            <p>未找到符合条件的学生信息</p>
        </div>
        <%
        }
        %>
    </div>
</body>
</html> 